package cn.iocoder.yudao.module.tms.dal.mysql.bandinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.tms.dal.dataobject.bandinfo.BandOrderDO;
import cn.iocoder.yudao.module.tms.controller.admin.bandinfo.vo.BandOrderPageReqVO;
import org.apache.ibatis.annotations.Mapper;

/**
 * Band 与产品关联 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface BandOrderMapper extends BaseMapperX<BandOrderDO> {

    default PageResult<BandOrderDO> selectPage(BandOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BandOrderDO>()
                .eqIfPresent(BandOrderDO::getBandCode, reqVO.getBandCode())
                .eqIfPresent(BandOrderDO::getOrderCode, reqVO.getOrderCode())
                .eqIfPresent(BandOrderDO::getCompanyId, reqVO.getCompanyId())
                .orderByDesc(BandOrderDO::getId));
    }

    /**
     * 统计指定 Band 下绑定的产品数量
     */
    default Long selectCountByBandCode(String bandCode) {
        return selectCount(BandOrderDO::getBandCode, bandCode);
    }

    /**
     * 统计指定 Band 下累计金额
     */
    default java.math.BigDecimal selectSumAmountByBandCode(String bandCode) {
        // SQL sum 查询
        java.util.List<java.util.Map<String, Object>> result = selectMaps(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<BandOrderDO>()
                        .select("SUM(amount) AS usedAmount")
                        .eq("band_code", bandCode));
        java.util.Map<String, Object> first = cn.hutool.core.collection.CollUtil.getFirst(result);
        java.lang.Double val = cn.hutool.core.map.MapUtil.getDouble(first, "usedAmount", 0D);
        return java.math.BigDecimal.valueOf(val);
    }

}